package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.dao.CompanyDao;
import cn.entity.TCompany;
import cn.entity.TStudent;
import cn.util.PageBean;
@Controller("companyAction")
@Scope("prototype")
public class CompanyAction extends BaseAction{
	private Integer id;
	private String name;
	private TCompany company=new TCompany();
	private List<TCompany> list;
	@Autowired
	@Qualifier("companyDao")
	private CompanyDao companyDao;

	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TCompany u where 1=1";
		String sql="from TCompany u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and u.name like '%"+name+"%'";
			sql+=" and u.name like '%"+name+"%'";	
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(companyDao.getCount(sqlCount));
		list=companyDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}
	
	public String add(){
		company.setIsdel(0);
		companyDao.add(company);
		return "list";
	}

	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TCompany com=companyDao.get(TCompany.class, id);		
		out.print(JSON.toJSONString(com));
		out.close();
		return null;
	}
	
	public String listJson() throws IOException{
		String sql="from TCompany u where 1=1";
		list=companyDao.getByHql(sql);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print(JSONArray.toJSONString(list));
		out.close();
		return null;
	}
	
	public String update(){
		TCompany com=companyDao.get(TCompany.class, id);
		if(company.getAddress()!=null && !"".equals(company.getAddress())){
			com.setAddress(company.getAddress());
		}
		if(company.getEmail()!=null && !"".equals(company.getEmail())){
			com.setEmail(company.getEmail());
		}
		if(company.getName()!=null && !"".equals(company.getName())){
			com.setName(company.getName());
		}
		if(company.getTel()!=null && !"".equals(company.getTel())){
			com.setTel(company.getTel());
		}
		
		companyDao.update(com);
		return "list";
	}
	
	
	
	public String delete(){
		TCompany s=companyDao.get(TCompany.class, id);
		s.setIsdel(1);
		companyDao.update(s);
		return "list";
	}
	


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TCompany getCompany() {
		return company;
	}

	public void setCompany(TCompany company) {
		this.company = company;
	}

	public List<TCompany> getList() {
		return list;
	}

	public void setList(List<TCompany> list) {
		this.list = list;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

}
